package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，一共六次请求
     * 1.查询套餐分类
     * 2.查询菜品分类
     * 3.查询菜品列表
     * 4.5.文件上传下载
     * 6.套餐保存
     */
    @PostMapping
    //删除一个缓存，从已有的缓存里删除一个类型，然后下次查询的时候重新添加缓存
    @CacheEvict(cacheNames = "setmealList",key = "#setmealDto.categoryId +'_'+#setmealDto.status")
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        log.info("添加成功");
        return R.success("新增成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        //1.先查询套餐分类数据
        //1.1分页查询
        Page<Setmeal> pageInfo1 = new Page<>(page, pageSize);
        //1.2查询条件
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByDesc("update_time");
        setmealService.page(pageInfo1, queryWrapper);
        //2. 将page<Setmeal> pageInfo1 转换为Page<SetmealDto> pageInfo2
        Page<SetmealDto> pageInfo2 = new Page<>();
        //    2.1拷贝除了records的所有数据
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");
        //    2.2拷贝records
        //    2.2.1设置接收SetmealDto的集合(因为records是集合，而设置是for循环一个一个设置，一个一个添加的)
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        //2.2.2使用for循环一个一个拷贝，并在其中添加分类名称
        for (Setmeal setmeal : pageInfo1.getRecords()) {
            //将每一个setmeal转换成setmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //    TODO 设置分类名称
            Category category = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            //    将每一个setmealDto添加到setmealDtoList中a
            setmealDtoList.add(setmealDto);
        }
        pageInfo2.setRecords(setmealDtoList);
        return R.success(pageInfo2);
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "steamlList",allEntries = true)
    public R<String> deleteByIds(Long ids[]) {
        setmealService.removeWithDish(Arrays.asList(ids));
        return R.success("删除成功");
    }

    /**
     * 根据ID查询(?)
     */
    @GetMapping("/{id}")
    public R<SetmealDto> findById(Long id) {
        //select * from setmeal where id=id
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //    TODO 设置分类名称
        Category category = categoryService.getById(setmealDto.getCategoryId());
        setmealDto.setCategoryName(category.getName());
        return R.success(setmealDto);
    }

    /**
     * 根据套餐ID查询套餐列表
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmealList", key = "#setmeal.categoryId +'_'+#setmeal.status", unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //select * from setmeal where category_id=? and status=?
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.eq("category_id", setmeal.getCategoryId());
        setmealQueryWrapper.eq("status", setmeal.getStatus());
        setmealQueryWrapper.orderByDesc("update_time");

        List<Setmeal> setmealList = setmealService.list(setmealQueryWrapper);
        return R.success(setmealList);
    }

    /**
     * 根据套餐ID查询套餐的菜品(未讲)
     */
    @GetMapping("/dish/{setmeal_id}")
    public R<List<SetmealDish>> findBySetmeal_id(@PathVariable("setmeal_id") Character setmeal_id) {
        //    select * from setmeal_dish where setmeal_id=?
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.eq("setmeal_id", setmeal_id);
        //setmealDishQueryWrapper.orderByDesc("sort");

        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishQueryWrapper);

        return R.success(setmealDishList);

    }
}
